*------------------------------------------------------------------------------
* PROGRAM: Replication code: Summary Statistics for Land Tenure & Deforestatoin
* DATE: 2/25/25
* You must set your own path for this to run.
*------------------------------------------------------------------------------

**SET $PATH HERE 

global treetablesuganda "$path/Outputs/Trees Experiment/Tables"
global treefiguresuganda "$path/Outputs/Trees Experiment/Figures"

use "$path/Data/Uganda_WTA_Trees_clean_anon.dta", replace 

recast byte C1 

* Create a few more final variables *
egen avg_share_cut=rowmean(share_cut_r1 share_cut_r2 share_cut_r3 share_cut_r4 share_cut_r5 share_cut_r6 share_cut_r7 share_cut_r8 share_cut_r9 share_cut_r10)

label variable avg_share_cut "Average share cut" 

*--------------------------------------------------------------------------*
* Trees cut by round, by treatment
*--------------------------------------------------------------------------*
preserve
reshape long share_cut_r trees_cut_r, i(HHID) j(round)

collapse (mean) mean_cut_r= trees_cut_r (sd) sd_cut_r=trees_cut_r (count)  n=trees_cut_r, by(trees_treat round)
generate hi_cut_r = mean_cut_r + invttail(n-1,0.05)*(sd_cut_r / sqrt(n))
generate lo_cut_r = mean_cut_r - invttail(n-1,0.05)*(sd_cut_r / sqrt(n))

twoway (line mean_cut_r round if trees_treat==1, color(forest_green) lpattern(_#)) ///
         (line mean_cut_r round if trees_treat==2, color(emerald) lpattern(dash)) ///
         (line mean_cut_r round if trees_treat==3, color(emerald)) ///
		 (rcap hi_cut_r lo_cut_r  round, color(gs13)), ///	
		 xtitle(Round) ytitle(Mean # Trees Cut) legend (order(1 "Insecure" 2 "Certificate" 3 "Secure" 4 "90% Confidence Intervals")) graphregion(color(white)) bgcolor(white) ylabel(1.5(.25)2.5) yline(2, lcolor(gs10) lpattern(dash)) xlabel(1(1)10)
		 
graph export "$treefiguresuganda/numtreescut.jpg", as(jpg) name("Graph") quality(90) replace

restore

*--------------------------------------------------------------------------*
* Share cut by round, by treatment
*--------------------------------------------------------------------------*

*** Tress Game: Share cut by treatment group and round (mean and standard error)

preserve 
drop if Finished==.
reshape long share_cut_r , i(HHID) j(round)
collapse (mean) meanshare_cut_r= share_cut_r (sd) sdshare_cut_r=share_cut_r (count)  n=share_cut_r, by(trees_treat round)
generate hishare_cut_r = meanshare_cut_r + invttail(n-1,0.05)*(sdshare_cut_r / sqrt(n))
generate loshare_cut_r = meanshare_cut_r - invttail(n-1,0.05)*(sdshare_cut_r / sqrt(n))

gen roundtreat=trees_treat if round==1
replace roundtreat=trees_treat+4 if round==2
replace roundtreat=trees_treat+8 if round==3
replace roundtreat=trees_treat+12 if round==4
replace roundtreat=trees_treat+16 if round==5
replace roundtreat=trees_treat+20 if round==6
replace roundtreat=trees_treat+24 if round==7
replace roundtreat=trees_treat+28 if round==8
replace roundtreat=trees_treat+32 if round==9
replace roundtreat=trees_treat+36 if round==10
sort roundtreat
list roundtreat round trees_treat, sepby(round)

twoway (bar meanshare_cut_r roundtreat if trees_treat==1, color(forest_green)) ///
         (bar meanshare_cut_r roundtreat if trees_treat==2, color(emerald)) ///
         (bar meanshare_cut_r roundtreat if trees_treat==3, color(green)) ///
         (rcap hishare_cut_r loshare_cut_r  roundtreat), ///
		 xlabel( 2 "1" 6 "2" 10 "3" 14 "4" 18 "5" 22 "6" 26 "7" 30 "8" 34 "9" 38 "10" , noticks) xtitle(Round) ytitle(Mean Share Cut) legend (order(1 "Insecure" 2 "Certificate" 3 "Secure"))
graph export "$treefiguresuganda/sharetreescut.jpg", as(jpg) name("Graph") quality(90) replace

twoway (line meanshare_cut_r roundtreat if trees_treat==1, color(gs2) lpattern(shortdash)) ///
         (line meanshare_cut_r roundtreat if trees_treat==2, color(gs7) lpattern(longdash)) ///
         (line meanshare_cut_r roundtreat if trees_treat==3, color(gs10)lpattern(solid)) ///
         (rcap hishare_cut_r loshare_cut_r  roundtreat, color(gs5)), ///
		 xlabel( 2 "1" 6 "2" 10 "3" 14 "4" 18 "5" 22 "6" 26 "7" 30 "8" 34 "9" 38 "10" , noticks) xtitle(Round) ytitle(Mean Share Cut) legend (order(1 "Insecure" 2 "Certificate" 3 "Secure" 4 "90% Confidence Intervals")) graphregion(color(white)) bgcolor(white) 
graph export "$treefiguresuganda/sharetreescutline.jpg", as(jpg) name("Graph") quality(90) replace
restore

*--------------------------------------------------------------------------*
* Summary Statistics
*--------------------------------------------------------------------------*

* Tress Game: Summary Statistics
*--------------------------------------------------------------------------*

local sumvars C8_1_TEXT female C1 C3 primaryschool secondaryschool C12_1_TEXT income_land acresland D1_1_TEXT E4_1_TEXT treecoverage cutstreescharc E8_1 E8_2 E8_3 B12a_1_TEXT consumption D6_1 D6_2  D6_3  D6_4  D6_5  D6_7  D7_7  risk patience trust mailo customary private trees_insecure trees_certificate trees_secure
estpost tabstat `sumvars', s(n mean sd min max ) col(stat)
esttab . using  "$treetablesuganda/summary.tex" ,  ///
c("count(fmt(%8.0f)) mean(fmt(%8.2f)) sd(fmt(%8.2f)) min(fmt(a3)) max(fmt(a3)) ") collabels("N" "Mean" "SD" "Min" "Max") ///
label noobs nodepvar nomtitle nonumber replace coeflabels() compress tex postfoot(\hline \hline \end{tabular} \begin{tablenotes} \item \footnotesize{\textit{Asset index} counts the total number of the following assets that the participant has in their home: radio, electricity, television, solar panel, electric stove, gas stove, internet access, mobile phone, bicycle, car/truck, and motorcycle.}; 0 otherwise. \end{tablenotes} )


* Tress Game: Balance of Covariates
*--------------------------------------------------------------------------*

* Variables: age, number ppl in home, number children,  education, sex, risk preference, wealth, land tenure

file open myfile using  "$treetablesuganda/balance.tex", write replace
file write myfile "\begin{tabular}{l c c c c c c } \hline \hline"
file write myfile " & (1) & (2) & (3) & \multicolumn{3}{c}{Normalized Differences} \\"
file write myfile " Variable &  Insecure & Certificate & Secure & (1)-(2) & (2)-(3) & (1)-(3) \\ \hline"


file write myfile "  \vspace{-0.2cm}  \\"


foreach v in C8_1_TEXT female C1 C3 primaryschool secondaryschool C12_1_TEXT income_land acresland D1_1_TEXT E4_1_TEXT treecoverage cutstreescharc E8_1 E8_2 E8_3 B12a_1_TEXT consumption D6_1 D6_2  D6_3  D6_4  D6_5  D6_7  D7_7  risk patience trust mailo customary private  {
	
	ttest `v'  if trees_treat<3, by(trees_treat)

if "`v'" == "E42_1_TEXT" {
		local mu1a = round(r(mu_1),1)
		local mu1 = string(`mu1a',"%8.0f")
		local mu2a =  round(r(mu_2),1)
		local mu2 = string(`mu2a',"%8.0f")
	} 
else {
		local mu1a = round(r(mu_1),0.001)
		local mu1 = string(`mu1a',"%8.3f")
		local mu2a =  round(r(mu_2),0.001)
		local mu2 = string(`mu2a',"%8.3f")
}

local diff1 = round((r(mu_1) - r(mu_2))/(sqrt(r(sd_1)^2 + r(sd_2)^2)),0.001)	
*local diff1 = `mu1'-`mu2'
local pval1 = r(p)

if "`v'" == "E42_1_TEXT" {
 if `pval1'>0.1  {
	local aa = string(`diff1') 
 } 
 else if `pval1'>0.05 { 
	local aa = string(`diff1') + "$^\star$"
 }
  else if `pval1'>0.01 { 
	local aa = string(`diff1') + "$^\star$" + "$^\star$"
 }
 else {
 	local aa = string(`diff1') + "$^\star$" + "$^\star$" + "$^\star$"
 }
}

else {
	 if `pval1'>0.1  {
	local aa = string(`diff1',"%8.3f") 
 } 
 else if `pval1'>0.05 { 
	local aa = string(`diff1',"%8.3f") + "$^\star$"
 }
  else if `pval1'>0.01 { 
	local aa = string(`diff1',"%8.3f") + "$^\star$" + "$^\star$"
 }
 else {
 	local aa = string(`diff1',"%8.3f") + "$^\star$" + "$^\star$" + "$^\star$"
 }
}

ttest `v'  if trees_treat>1, by(trees_treat)

if "`v'" == "E42_1_TEXT" {
	local mu3a =  round(r(mu_2),1)
	local mu3 = string(`mu3a',"%8.0f")	
} 
else {
	local mu3a =  round(r(mu_2),0.001)
	local mu3 = string(`mu3a',"%8.3f")
}

local diff2 = round((r(mu_1) - r(mu_2))/(sqrt(r(sd_1)^2 + r(sd_2)^2)),0.001)	
*local diff2 =  `mu2'-`mu3'
local pval2 = r(p)

if "`v'" == "E42_1_TEXT" {
 if `pval2'>0.1  {
	local bb = string(`diff2') 
 } 
 else if `pval2'>0.05 { 
	local bb = string(`diff2') + "$^\star$"
 }
  else if `pval2'>0.01 { 
	local bb = string(`diff2') + "$^\star$" + "$^\star$"
 }
 else {
 	local bb = string(`diff2') + "$^\star$" + "$^\star$" + "$^\star$"
 }
}

else {
	 if `pval2'>0.1  {
	local bb = string(`diff2',"%8.3f") 
 } 
 else if `pval2'>0.05 { 
	local bb = string(`diff2',"%8.3f") + "$^\star$"
 }
  else if `pval2'>0.01 { 
	local bb = string(`diff2',"%8.3f") + "$^\star$" + "$^\star$"
 }
 else {
 	local bb = string(`diff2',"%8.3f") + "$^\star$" + "$^\star$" + "$^\star$"
 }
}
 
ttest `v'  if trees_treat==1|trees_treat==3, by(trees_treat) 

local diff3 = round((r(mu_1) - r(mu_2))/(sqrt(r(sd_1)^2 + r(sd_2)^2)),0.001)	
*local diff3 =  `mu1'-`mu3'
local pval3 = r(p)

if "`v'" == "E42_1_TEXT" {
 if `pval3'>0.1  {
	local cc = string(`diff3') 
 } 
 else if `pval3'>0.05 { 
	local cc = string(`diff3') + "$^\star$"
 }
  else if `pval3'>0.01 { 
	local cc = string(`diff3') + "$^\star$" + "$^\star$"
 }
 else {
 	local cc = string(`diff3') + "$^\star$" + "$^\star$" + "$^\star$"
 }
}

else {
	 if `pval3'>0.1  {
	local cc = string(`diff3',"%8.3f") 
 } 
 else if `pval3'>0.05 { 
	local cc = string(`diff3',"%8.3f") + "$^\star$"
 }
  else if `pval3'>0.01 { 
	local cc = string(`diff3',"%8.3f") + "$^\star$" + "$^\star$"
 }
 else {
 	local cc = string(`diff3',"%8.3f") + "$^\star$" + "$^\star$" + "$^\star$"
 }
}

local lbl: variable label `v'
file write myfile "`lbl'  & `mu1' &  `mu2' & `mu3' & `aa' & `bb' & `cc' \\"

capture macro drop `mu1'  `mu2'  `mu3'  `aa' `bb'  `cc' `diff1' `diff2' `diff3'

}

file write myfile "\hline \hline \end{tabular} " _n
file write myfile "\begin{tablenotes} \item \scriptsize{* p$<$ 0.10, ** p$<$0.05, *** p $<$ 0.01. Normalized differences are interpreted in standard deviations. Differences less than $|0.25|$ standard deviations do not bias treatment effects, even when statistically significant \citep{imbens2015causal}. \textit{Asset index} counts the total number of the following assets that the participant has in their home: radio, electricity, television, solar panel, electric stove, gas stove, internet access, mobile phone, bicycle, car/truck, and motorcycle.} \end{tablenotes}"
file close myfile



*--------------------------------------------------------------------------*
* Balance for buy certificate v. did not buy certificate
*--------------------------------------------------------------------------*
file open myfile using  "$treetablesuganda/certificate_ttest.tex", write replace
file write myfile "\begin{tabular}{l c c c} \hline \hline"
file write myfile " Variable &  No Certificate & Certificate & Normalized Difference \\ \hline"


file write myfile "  \vspace{-0.2cm}  \\"

*** SW TRYING HERE WITH NORMALIZED DIFFERENCES TO SEE IF THESE MAGNITUDES ARE MEANIGFUL
egen vmailo = mean(mailo)
egen vcustomary = mean(customary)
egen vprivate = mean(private)

label var vmail "Village share mailo"
label var vcustomary "Village share customary"
label var vprivate "Village share private"

foreach v in C8_1_TEXT female C1 C3 primaryschool secondaryschool C12_1_TEXT income_land acresland D1_1_TEXT E4_1_TEXT treecoverage cutstreescharc E8_1 E8_2 E8_3 B12a_1_TEXT consumption D6_1 D6_2  D6_3  D6_4  D6_5  D6_7  D7_7  risk patience trust mailo customary private  {
	
	ttest `v', by(certbuy)

if "`v'" == "E42_1_TEXT" {
		local mu1a = round(r(mu_1),1)
		local mu1 = string(`mu1a',"%8.0f")
		local mu2a =  round(r(mu_2),1)
		local mu2 = string(`mu2a',"%8.0f")
	} 
else {
		local mu1a = round(r(mu_1),0.001)
		local mu1 = string(`mu1a',"%8.3f")
		local mu2a =  round(r(mu_2),0.001)
		local mu2 = string(`mu2a',"%8.3f")
}

local diff1 = round((r(mu_1) - r(mu_2))/(sqrt(r(sd_1)^2 + r(sd_2)^2)),0.001)	
*local diff1 = `mu1'-`mu2'
local pval1 = r(p)

if "`v'" == "E42_1_TEXT" {
 if `pval1'>0.1  {
	local aa = string(`diff1') 
 } 
 else if `pval1'>0.05 { 
	local aa = string(`diff1') + "$^\star$"
 }
  else if `pval1'>0.01 { 
	local aa = string(`diff1') + "$^\star$" + "$^\star$"
 }
 else {
 	local aa = string(`diff1') + "$^\star$" + "$^\star$" + "$^\star$"
 }
}

else {
	 if `pval1'>0.1  {
	local aa = string(`diff1',"%8.3f") 
 } 
 else if `pval1'>0.05 { 
	local aa = string(`diff1',"%8.3f") + "$^\star$"
 }
  else if `pval1'>0.01 { 
	local aa = string(`diff1',"%8.3f") + "$^\star$" + "$^\star$"
 }
 else {
 	local aa = string(`diff1',"%8.3f") + "$^\star$" + "$^\star$" + "$^\star$"
 }
}

local lbl: variable label `v'
file write myfile "`lbl'  & `mu1' &  `mu2' & `aa' \\"

capture macro drop `mu1' `mu2' `aa' `diff1'

}

file write myfile "\hline \hline \end{tabular} " _n
file write myfile "\begin{tablenotes} \item \footnotesize{* p$<$ 0.10, ** p$<$0.05, *** p $<$ 0.01. Compares normalized differences in means for respondents in the certificate treatment group who purchased a certicate vs. did not purchase a certificate. \textit{Asset index} counts the total number of the following assets that the participant has in their home: radio, electricity, television, solar panel, electric stove, gas stove, internet access, mobile phone, bicycle, car/truck, and motorcycle.} \end{tablenotes}"
file close myfile

